<template>
  <div class="transfer-view">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item
        label="当前账户号:"
        prop="from"
        :rules="[
          {
            required: true,
            message: '请输入账户号',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="ruleForm.from" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="目标账户号:"
        prop="to"
        :rules="[
          {
            required: true,
            message: '请输入目标账户号',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model="ruleForm.to" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="转账金额:"
        prop="money"
        :rules="[
          {
            required: true,
            message: '请输入转账金额',
            trigger: 'blur',
          },
        ]"
      >
        <el-input v-model.number="ruleForm.money" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import axios from "axios";

const ruleForm = reactive({
  from: "",
  to: "",
  money: "",
});

const ruleFormRef = ref<FormInstance>();

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  formEl.validate((valid) => {
    if (valid) {

      axios({
        url: "http://localhost:9090/transfer",
        data: ruleForm,
        method: "POST",
      }).then((res) => {

        if (res.status) {
          ElMessage({
            message: res.data,
            grouping: true,
            type: "success",
          });
        }
      });

      console.log("submit!");
    } else {
      console.log("error submit!");
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped></style>
